package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.annotation.SuppressLint
import android.graphics.Paint
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.util.ImageLoaderManager
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityAboutThisOrgBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterAboutThisOrg
import com.gexiaobao.pigeon.viewmodel.OrgRaceViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/7/17 14:35
 * @Describe    : 关于本公棚
 */
class ActivityAboutThisOrg : BaseActivity<OrgRaceViewModel, ActivityAboutThisOrgBinding>() {

    private var raceId = 0
    private var memberId = 0

    private var mOrgName = ""
    private var mOrgIcon = ""
    private var mOrgRaceName = ""

    private val mAdapter: AdapterAboutThisOrg by lazy { AdapterAboutThisOrg(arrayListOf()) }

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = getString(R.string.About_the_cottage)
        mDatabind.tvAboutOrgPigeons.paint.flags = Paint.UNDERLINE_TEXT_FLAG

        raceId = intent.getIntExtra("raceId", 0)
        memberId = intent.getIntExtra("memberId", 0)

        mOrgIcon = intent.getStringExtra("orgIcon").toString()
        mOrgName = intent.getStringExtra("orgName").toString()
        mOrgRaceName = intent.getStringExtra("orgRaceName").toString()

        mDatabind.includeOrg.tvIncludeOrgName.text = mOrgName
        mDatabind.includeOrg.tvIncludeOrgRaceName.text = mOrgRaceName
        ImageLoaderManager.loadCircleImageFall(this, mOrgIcon, mDatabind.includeOrg.ivOrgIcon, R.mipmap.profilepic_str_1)

        onRefresh()

        mDatabind.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter)
            .addItemDecoration(SpaceItemDecoration(0, Util.dp2px(this, 6f)))

        mDatabind.swipeRefreshLayout.init {
            onRefresh()
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            startActivity<ActivityAboutThisPass>(
                "passId" to mAdapter.data[position].passId,
                "memberId" to memberId,
                "orgName" to mOrgName,
                "orgIcon" to mOrgIcon,
                "orgRaceName" to mOrgRaceName
            )
        }
    }

    private fun onRefresh() {
        showLoading(getString(R.string.loading))
        mViewModel.getMemberLoftData(raceId, memberId)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        mViewModel.aboutLoftResult.observe(this) {
            if (mDatabind.swipeRefreshLayout.isRefreshing) {
                mDatabind.swipeRefreshLayout.isRefreshing = false
            }
            mDatabind.data = it.memberLoftData
            mAdapter.setList(it.memberLoftPassData)
            mAdapter.notifyDataSetChanged()
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack, mDatabind.llAboutOrgPigeons) {
            when (it) {
                mDatabind.llAboutOrgPigeons -> {
                    startActivity<ActivityNumberOfPigeonDelivered>("memberId" to memberId)
                }

                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }
}